package com.example.hasee.plant.fragment;

import android.content.Intent;
import android.graphics.Bitmap;
import android.media.ThumbnailUtils;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.hasee.plant.R;
import com.example.hasee.plant.activity.TestBasicVideo;
import com.example.hasee.plant.config.MyApplication;
import com.example.hasee.plant.utils.BitmapUtil;

import java.io.File;

/**
 * Created by jyb on 2017/2/2.
 */

public class GatherVideoFragment extends Fragment {
    private View view;
    private ImageView iv_video,iv_play;
    private TextView tv_startVideo;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view=inflater.inflate(R.layout.gather_video_frag,null);
        iv_video= (ImageView) view.findViewById(R.id.iv_video);
        iv_play= (ImageView) view.findViewById(R.id.iv_play);
        tv_startVideo= (TextView) view.findViewById(R.id.tv_startVideo);
        onListener();
        return view;
    }

    private void onListener(){
        tv_startVideo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(MyApplication.VIDEO_PATH_FINISH!=null){
                    File file=new File(MyApplication.VIDEO_PATH_FINISH);
                    if(file.exists()){
                        file.delete();
                    }
                }
                Intent intent=new Intent(getActivity(), TestBasicVideo.class);
                startActivityForResult(intent,3);
            }
        });

        iv_play.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(getActivity(), TestBasicVideo.class);
                intent.putExtra("isPlay",true);
                if(!videoPath.equals("")){
                    intent.putExtra("videoPath",videoPath);
                    startActivity(intent);
                }else{
                    Toast.makeText(getActivity(),"获取视频文件路径失败",Toast.LENGTH_SHORT).show();
                }

            }
        });


    }

    String videoPath="";
    Bitmap bitmap;
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(resultCode==getActivity().RESULT_OK){
            if(data!=null){
                videoPath=data.getStringExtra("videoPath");
                if(videoPath!=null){
                    MyApplication.VIDEO_PATH_FINISH=videoPath;
                    bitmap= BitmapUtil.getVideoThumbnail(videoPath,500,500, MediaStore.Video.Thumbnails.MINI_KIND);
                    iv_video.setImageBitmap(bitmap);
                    iv_play.setVisibility(View.VISIBLE);
                }
            }else{
                Toast.makeText(getActivity(),"返回数据为空",Toast.LENGTH_SHORT).show();
            }
        }else{
            Toast.makeText(getActivity(), "获取图片失败", Toast.LENGTH_SHORT).show();
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}
